<template>
	<view class="sign_in">
		<view class="main">
			<view class="title">修改登录密码</view>
			
			<view class="input">
				<u--input
					:password="!showPass"
					type="text"
				    placeholder="请输入8-16位数字、英文组合密码"
				    border="surround"
				    v-model="pass"
				  >
					<template slot="suffix">
						<view @click="showPass = !showPass" class="eye-icon">
							<u-icon 
								name="eye-fill" 
								color="#909399"
								v-if="showPass"
							></u-icon>
							<u-icon 
								name="eye-off" 
								color="#909399"
								v-else
							></u-icon>
						</view>
					</template>
				</u--input>
			</view>
			<view class="input">
				<u--input
					:password="!showPassTwo"
					type="text"
				    placeholder="请再次输入8-16位数字、英文组合密码"
				    border="surround"
				    v-model="pass_two"
				  >
					<template slot="suffix">
						<view @click="showPassTwo = !showPassTwo" class="eye-icon">
							<u-icon 
								name="eye-fill" 
								color="#909399"
								v-if="showPassTwo"
							></u-icon>
							<u-icon 
								name="eye-off" 
								color="#909399"
								v-else
							></u-icon>
						</view>
					</template>
				</u--input>
			</view>
			
			<view class="input">				
				<!-- #ifndef APP-NVUE -->
				<u-input placeholder="请输入手机号" v-model="phone">
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				<u--input placeholder="请输入手机号" v-model="phone">
				<!-- #endif -->
					<template slot="suffix">
						<u-code
							ref="uCode"
							@change="codeChange"
							:seconds="sendTime"
							changeText="X秒重新获取"
						></u-code>
						<u-button
							@tap="getCode"
							:text="tips"
							type="primary"
							size="mini"
						></u-button>
					</template>
				<!-- #ifndef APP-NVUE -->
				</u-input>
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				</u--input>
				<!-- #endif -->
				
			</view>
			<view class="input">
				<u--input
				    placeholder="请输入验证码"
				    border="surround"
				    v-model="code"
				  ></u--input>
			</view>
			<view class="in_btn" @click="submitBtn">确认修改</view>
			<!-- <view class="bot_txt" @click="siginBtn">登录</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone:'',
				code:'',
				pass:'',
				pass_two:'',
				// 控制密码显示/隐藏（默认隐藏）
				showPass: false,
				showPassTwo: false,
				// 
				disabled:false,
				codeTxt:'发送验证码',
				sendTime:60,
				time:null,
				tips: '',
				value: ''
			};
		},
		methods:{
			codeChange(text) {
				this.tips = text;
			  },
			  getCode() {
				if (this.$refs.uCode.canGetCode) {
				  // 模拟向后端请求验证码
				  this.sendCodeBtn();
				} else {
				  uni.$u.toast('倒计时结束后再发送');
				}
			  },
			  change(e) {
				console.log('change', e);
			  },
			// 发送验证码
			sendCodeBtn(){
				var that = this
				if(that.disabled){
					return
				}
				if(that.phone=='' || !that.$test.phoneTrue(that.phone)){
					that.$tips.showToast('请输入正确的手机号')
					return
				}else{
					that.$tips.showLoading('发送中')
					var params = {
						mobile:that.phone,
						event:'resetpwd'
					}
					that.$httpApi.smsSend(params).then((res)=>{
						that.$tips.hideLoading()
						if(res.code==1){
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
							if (!that.timer) {
							  that.disabled = true
							  that.codeTxt = that.sendTime+'s'
							  that.timer = setInterval(() => {
							    if (that.sendTime > 0 && that.sendTime <= 60) {
							      that.sendTime--
							      that.codeTxt = that.sendTime+'s'
							    } else {
							      that.disabled = false
							      that.sendTime = 60
							      that.codeTxt = '重新获取'
							      clearInterval(that.timer)
							      that.timer = null;
							    }
							  }, 1000)
							}
						}
					})
				}
			},
			submitBtn(){
				var that = this
				if(that.phone=='' || !that.$test.phoneTrue(that.phone)){
					that.$tips.showToast('请输入正确的手机号')
					return
				}else if(that.code==''){
					that.$tips.showToast('请输入验证码')
					return
				}else if(that.pass==''){
					that.$tips.showToast('请输入密码')
					return
				}else if(that.pass.length<8 || that.pass.length>16){
					that.$tips.showToast('密码格式不正确')
					return
				}else if(that.pass_two==''){
					that.$tips.showToast('请再次输入密码')
					return
				}else if(that.pass!=that.pass_two){
					that.$tips.showToast('密码不一致')
					return
				}else{
					that.$tips.showLoading('加载中')
					var params = {
						mobile:that.phone,
						captcha:that.code,
						newpassword:that.pass,
						newpassword_tow:that.pass_two,
					}
					that.$httpApi.userResetpwd(params).then((res)=>{
						that.$tips.hideLoading()
						if(res.code==1){
							that.$tips.showToast('修改成功')
							setTimeout(function(){
								uni.reLaunch({
									url:'/pages/signIn/sign_in'
								})
							},2000)
						}
					})
				}
			},
			// 登录
			siginBtn(){
				uni.navigateBack()
			},
		}
	}
</script>

<style lang="less">
	.sign_in{
		width: 100%;
		height: 100vh;
		.main{
			position: relative;
			z-index: 2;
			width: 100%;
			padding: 200rpx 65rpx 50rpx;
			box-sizing: border-box;
			.title{
				width: 100%;
				font-size: 50rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: 200;
				color: #666;
				padding-bottom: 100rpx;
			}
			.input{
				margin-bottom: 40rpx;
				background:rgba(190, 190, 190, 0.1);
				/* 自定义眼睛图标的样式：扩大点击区域 */
				.eye-icon {
					padding: 0 10rpx;
					cursor: pointer;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
			.in_btn{
				margin-top: 100rpx;
				width: 100%;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				color: #fff;
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				background: var(--color-primary);
				border-radius: 20rpx;
			}
			.bot_txt{
				width: 100%;
				margin-top: 100rpx;
				text-align: center;
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				color: var(--color-primary);
			}
		}
	}
</style>